# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : driver_editor.py
# Date      : 2018/12/12
# IDE       : PyCharm
# Version   : 1.1.2
# -------------------------------


__author__ = "lin lingwei"


__version__ = "1.1.2"

import configs.constants.facial_constant as fc
import configs.constants.global_constant as gc
import configs.constants.suffix_constant as sc
import configs.constants.nodeType_constant as ntc
import configs.constants.attribute_constant as ac


import maya.cmds as mc
import scripts.commonCmds as cCore
import editors.editor as editor
reload(editor)
import scripts.deformerCmds as dCore
reload(dCore)

import scripts.driverCmds as driverToolkit
reload(driverToolkit)

__mayaVersion__ = mc.about(v=True)

class DriverEditor(editor.Editor):
    
    def __init__(self):
        super(DriverEditor,self).__init__()
    
    def editor(self):
        win = self.selfWindow()
        mc.window(win,e=True,wh=[280, 170])
        self.mainMenuLayout()
        
        self.mainLayout()
        mc.showWindow(win)
    
    def mainMenuLayout(self):
        _main = mc.menuBarLayout()
        mc.menu(l="Settings ...")
        mc.menuItem(l="Import 4/8 Example ...",c=lambda *args:driverToolkit.SoliDrive_class().createTestExample(0))
        mc.menuItem(l="Import 8/8 Example ...",c=lambda *args:driverToolkit.SoliDrive_class().createTestExample(1))

        return _main
    def mainLayout(self):
        
        _main = mc.columnLayout(adj=True)
        prefix = mc.textFieldGrp(l="Prefix :  ",tx="L_arm",cw=[1,50],adj=2,w=280)
        mc.separator()
        mc.rowLayout(nc=10)
        model = mc.radioCollection()
        rb4 = mc.radioButton("d4_8",label='4/8',w=70,sl=True)
        rb5 = mc.radioButton('d8_8',label='8/8',w=70)
        mc.setParent('..')
        mc.separator()

        mc.button(l=" - Create Driver - ",c=lambda *args:self.btn_create(prefix,model))
        mc.separator()
        bridgeTf = "bridgeTf"
        mc.textFieldButtonGrp(bridgeTf,l="Bridge :  ",cw=[1,50],adj=2,w=280,bl="Load",ed=False,
                              bc=lambda *args:self.btn_mirrorLoad(bridgeTf))
        mc.rowLayout(nc=10)
        axial = mc.radioCollection()
        rb4 = mc.radioButton("X",label='X',w=70,sl=True)
        rb5 = mc.radioButton('Y',label='Y',w=70)
        rb6 = mc.radioButton('Z',label='Z',w=70)
        # neg = mc.checkBox("- /neg", v=False,w=70)
        mc.setParent('..')
        mc.separator()

        mc.button(l=" - Mirror -    ",c = lambda *args:self.btn_Mirror(bridgeTf,axial))
        
        mc.separator()

        self.copyright()
        mc.setParent('..')
        return _main
    
    def btn_create(self,prefixTf,model):
        
        reload(driverToolkit)
        prefix = mc.textFieldGrp(prefixTf,q=True,tx=True)
        mo = mc.radioCollection(model,q=True,sl=True)
        dt = driverToolkit.SoliDrive_class(prefix)
        # print(mo)
        if mo == "d4_8":
            dt.do(0)
        elif mo == "d8_8":
            dt.do(1)

        return
    
    def btn_mirrorLoad(self,bridgeTf):
        
        mc.textFieldButtonGrp(bridgeTf,e=True,tx=mc.ls(sl=True)[0])
        return
    
    
    
    def btn_Mirror(self,bridgeTf,axialRadio):
        
        bridge = mc.textFieldButtonGrp(bridgeTf,q=True,tx=True)
        axial = mc.radioCollection(axialRadio,q=True,sl=True)
        # neg = mc.checkBox(negCb,q=True,v=True)
        prefix = bridge.split("_solidDrive_settings")[0]
        
        if gLeft+"_" in prefix:
            nn = prefix.replace(gLeft+"_",gRight+"_")
        
        elif gRight+"_" in prefix:
            nn = prefix.replace(gRight+"_",gLeft+"_")
        else:
            nn = cCore.lname(prefix,"Mirror")
            
        if mc.objExists(cCore.lname(nn,"solidDrive_settings")):
            mc.error("Driver Toolkit: {} mirrored.".format(bridge))
            return
        dt = driverToolkit.SoliDrive_class(nn)
        dt.do()
        
        
        orgPt = bridge.replace("_solidDrive_settings","_solidDrive_grp")
        
        tmploc = cCore.objToLocator(orgPt,"temp",parent=False)
        mg = mc.group(em=True)
        mc.parent(tmploc, mg)
        
        if axial == "X":
            mc.setAttr(mg + ".sx", -1)
        elif axial == "Y":
            mc.setAttr(mg + ".sy", -1)
        elif axial == "Z":
            mc.setAttr(mg + ".sz", -1)

        mc.parent(tmploc, w=True)
        mc.delete(mg)
        
        mc.delete(mc.parentConstraint(tmploc,nn+"_solidDrive_grp",mo=False))
        mc.setAttr("%s_Z_Axis.tz" % nn, -1)
        
        nBridge = cCore.lname(nn,"solidDrive_settings")
        attr = "min_angle"
        mc.setAttr(nBridge+"."+attr,mc.getAttr(bridge+"."+attr))
        attr = "max_angle"
        mc.setAttr(nBridge+"."+attr,mc.getAttr(bridge+"."+attr))
        attr = "min_driver_value"
        mc.setAttr(nBridge+"."+attr,mc.getAttr(bridge+"."+attr))
        attr = "max_driver_value"
        mc.setAttr(nBridge+"."+attr,mc.getAttr(bridge+"."+attr))
        
        mc.delete(tmploc)
        return
def showEditor():
    DriverEditor().editor()
    return
if __name__ == '__main__':
    pass